package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageView;

public class uc3 extends AppCompatActivity {

    private ImageView currentImage;
    private Handler handler;
    private Runnable runnable;
    private int[] imageResIds = {
            R.drawable.image1,  // 确保这些资源真实存在
            R.drawable.image2,
            R.drawable.image3,
            R.drawable.image4,
            R.drawable.image5
    };
    private int currentIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uc3);

        currentImage = findViewById(R.id.current_image);
        handler = new Handler(Looper.getMainLooper());

        initImageRotation();
    }

    private void initImageRotation() {
        runnable = new Runnable() {
            @Override
            public void run() {
                currentIndex = (currentIndex + 1) % imageResIds.length;
                currentImage.setImageResource(imageResIds[currentIndex]);
                handler.postDelayed(this, 1000); // 每秒切换
            }
        };
        handler.postDelayed(runnable, 1000); // 启动轮播
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (handler != null && runnable != null) {
            handler.removeCallbacks(runnable); // 防止内存泄漏
        }
    }
}